﻿#define _CRT_SECURE_NO_WARNINGS 1

#include"snake.h"

//设置光标坐标函数
void SetPos(int x, int y)
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos = { x,y };
	SetConsoleCursorPosition(handle, pos);
}
//打印欢迎信息
void welcomeToGame()
{
	//定位一个光标坐标
	SetPos(38, 12);
	printf("欢迎来到贪吃蛇小游戏\n");

	SetPos(40, 20);//调整“请按任意键继续...”的打印位置
	system("pause");
	system("cls");//清屏命令
	
	//打印下一个界面
	//打印功能介绍信息
	SetPos(26, 10);
	printf("用↑↓← →键进行移动操作，F3是加速，F4是减速\n ");
	SetPos(37, 12);
	printf("加速能得到更高的分数");
	SetPos(40, 20);//调整“请按任意键继续...”的打印位置
	system("pause");
	system("cls");

}
//绘制地图
void CreatMap()
{
	//上
	SetPos(0, 0);
	int i = 0;
	//wchar_t ch = L'■';
	for (i = 0; i <= 56; i += 2)//每两个字符打印的一个宽字符
	{
		wprintf(L"%lc", WALL);//定义在头文件里
	}
	//下 
	SetPos(0, 26);
	for (i = 0; i <= 56; i += 2)//每两个字符打印的一个宽字符
	{
		wprintf(L"%lc", WALL);//定义在头文件里
	}
	//左
	for ( i = 1; i <26 ; i++)
	{
		SetPos(0, i);
		wprintf(L"%lc", WALL);
	}
	//右
	for ( i = 1; i < 26; i++)
	{
		SetPos(56, i);
		wprintf(L"%lc", WALL);
	}
	//getchar();
}

//初始化贪吃蛇
void InitSnake(pSnake ps)
{
	//假设初始化时有五个横着的节点，起始位置固定
	pSnakeNode cur = NULL;
	for (int i = 0; i < 5; i++)
	{
		cur = (pSnakeNode)malloc(sizeof(SnakeNode));
		if (cur==NULL)
		{
			perror("InitSnake(pSnake ps):malloc()");
			return;
		}
		cur->x = POS_X + 2*i;
		cur->y = POS_Y;
		cur->next = NULL; 

		//头插法：新创建的节点为头
	    //当链表里一个节点都还没有时
		if (ps->pSnake == NULL)
		{
			ps->pSnake = cur;
		}
		else//已经有节点存在时
		{
			cur->next = ps->pSnake;
			ps->pSnake = cur;
		}
	}
	
	
	//打印蛇身：遍历
	cur = ps->pSnake;//从蛇头开始
	while (cur)
	{
		SetPos(cur->x, cur->y);//定位坐标
		wprintf(L"%lc", BODY);
		cur = cur->next;//最后一个节点的next为空
	}

	//贪吃蛇的其他信息初始化
	ps->dir = RIGHT;//初始默认方向向右
	ps->FoodWeight = 10;
	ps->pFood = NULL;
	ps->Score = 0;
	ps->SleepTime = 200;
	ps->status = OK;
	//getchar();

}

//创建食物
void CreateFood(pSnake ps)
{
	int x = 0;
	int y = 0;

again:
	do 
	{
		x = rand() % 53 + 2;
		y = rand() % 25 + 1;

	} while (x%2!=0);//x的坐标应该是2的倍数

	//坐标和蛇的身体的每个节点的坐标比较
	pSnakeNode cur = ps->pSnake;
	while (cur)
	{
		if (x == cur->x && y == cur->y)
		{
			goto again;
		}
		cur = cur->next;
	}

	//创建食物
	pSnakeNode pFood = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (pFood==NULL)
	{
		perror("CreateFood()::malloc()");
	}
	pFood->x = x;
	pFood->y = y;

	ps->pFood = pFood;
	SetPos(x, y);
	wprintf(L"%lc", FOOD);
	//getchar();

}

//游戏开始前准备
void GameStart(pSnake ps)
{
	//设置控制台的信息:大小、名称
	system("mode con cols=100 lines=32");
	system("title 贪吃蛇");
	
	//隐藏光标 
	HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO CursorInfo;
	GetConsoleCursorInfo(handle, &CursorInfo);//获取光标信息
	CursorInfo.bVisible = false;
	SetConsoleCursorInfo(handle, &CursorInfo);//设置光标信息
	
	//打印欢迎信息
	welcomeToGame();
	//绘制地图
	CreatMap();
	
	//初始化蛇
	InitSnake(ps);
	//创建食物 
	CreateFood(ps);

	//getchar();//防止程序结束看不到效果
	
}

//打印游戏信息
void PrintHelpInfo()
{
	SetPos(63, 15);
	printf("1.不能穿墙，不能咬到自己");
	SetPos(63, 16);
	printf("2.用↑↓← →键进行移动操作");
	SetPos(63, 17);
	printf("3.F3是加速，F4是减速");
	SetPos(63, 18);
	printf("4.加速能得到更高的分数");

}
//暂停
void pause()
{
	while (1)
	{
		Sleep(100);
		if (KEY_PRESS(VK_SPACE))
		{
			break;
		}
	}

}
//判断蛇头下一个位置是不是食物
int Next_Is_Food(pSnake ps, pSnakeNode  pNext)
{
	if (ps->pFood->x == pNext->x && ps->pFood->y == pNext->y)
		return 1;//下一个坐标处是食物
	else
		return 0;
}
//下一步吃食物
void EatFood(pSnake ps, pSnakeNode  pNext)
{
	pNext->next = ps->pSnake;
	ps->pSnake = pNext;
	
	pSnakeNode cur = ps->pSnake;
	//打印蛇身
	while (cur)
	{
		SetPos(cur->x,cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}
	//加分
	ps->Score += ps->FoodWeight;
	//释放旧的食物
	free(ps->pFood);
	//创建新的食物
	CreateFood(ps);
}
//下一步不吃食物
void NotEatFood(pSnake ps, pSnakeNode  pNext)
{
	//头插法
	pNext->next = ps->pSnake;
	ps->pSnake = pNext;

	//释放尾节点
	pSnakeNode cur = ps->pSnake;
	while (cur->next->next!=NULL)//找到倒数第二个节点
	{
		//遍历的同时打印蛇身
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}
	SetPos(cur->x, cur->y);
	wprintf(L"%lc", BODY);
	
	//将尾节点打印成空白字符,先打印后销毁
	SetPos(cur->next->x, cur->next->y);
	printf("  ");

	//销毁尾节点
	free(cur->next);
	cur->next = NULL;

}
//检测是否撞墙
void KillByWall(pSnake ps)
{
	if (ps->pSnake->x==0 ||
		ps->pSnake->x==56||
		ps->pSnake->y==0 ||
		ps->pSnake->y==26)
	{
		ps->status = KILL_BY_WALL;
	}
}

//检测是否撞自己
void KillBySelf(pSnake ps)
{
	pSnakeNode cur = ps->pSnake->next;
	while (cur)
	{
		if (cur->x == ps->pSnake->x && cur->y == ps->pSnake->y)
		{
			ps->status = KILL_BY_SELF;
			
		}
		cur = cur->next;
	}
	return;
}
//移动一步
void SnakeMove(pSnake ps)
{
	//创建一个节点，设为下一个位置的坐标
	pSnakeNode pNext = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (pNext == NULL)
	{
		perror("SnakeMove()::malloc()");
		return;

	}
	pNext->next = NULL;

	switch (ps->dir)
	{
	case UP:
		pNext->x = ps->pSnake->x;
		pNext->y = ps->pSnake->y - 1;
		break;
	case DOWN:
		pNext->x = ps->pSnake->x;
		pNext->y = ps->pSnake->y + 1;
		break;
	case LEFT:
		pNext->x = ps->pSnake->x - 2;
		pNext->y = ps->pSnake->y;
		break;
	case RIGHT:
		pNext->x = ps->pSnake->x + 2;
		pNext->y = ps->pSnake->y;
		break;
	}
	//下一个坐标是不是食物
	if (Next_Is_Food(ps, pNext))
	{
		//是食物就吃掉
		EatFood(ps, pNext);
	}
	else
	{
		//不是食物就正常一步
		NotEatFood(ps,pNext);
	}

	//检测是否撞墙
	KillByWall(ps);

	//检测是否撞自己
	KillBySelf(ps);


}
//游戏进行
void GameRun(pSnake ps)
{
	//打印帮助信息
	PrintHelpInfo();

	do
	{
		//当前的分数情况
		SetPos(63, 10);
		printf("当前分数：%5d\n", ps->Score);
		SetPos(63, 11);
		printf("食物的分值：%02d\n", ps->FoodWeight);//初始的分是10，如果变成8只会改变第一个字符，第二个字符的0依旧会在，结果变维80
		//检测按键
		//上、下、左、右、ESC、空格、F3、F4
		if (KEY_PRESS(VK_UP) && ps->dir!=DOWN)//上键
		{
			ps->dir = UP;
		}
		else if (KEY_PRESS(VK_DOWN) && ps->dir != UP)
		{
			ps->dir = DOWN;
		}
		else if (KEY_PRESS(VK_LEFT) && ps->dir != RIGHT)
		{
			ps->dir = LEFT;
		}
		else if (KEY_PRESS(VK_RIGHT) && ps->dir != LEFT)
		{
			ps->dir = RIGHT;
		}
		else if (KEY_PRESS(VK_ESCAPE))
		{
			ps->status = ESC;
			break;
		}
		else if (KEY_PRESS(VK_SPACE))
		{
			//游戏要暂停
			pause();//暂停和恢复暂停
		}
		else if (KEY_PRESS(VK_F3))//加速，休眠时间要变短
		{
			//初始的休眠时间是200
			if (ps->SleepTime >= 80)//最快速度
			{
				ps->SleepTime -= 30;
				ps->FoodWeight += 2;
			}
		}
		else if (KEY_PRESS(VK_F4))//减速，休眠时间变长
		{
			if (ps->FoodWeight > 2)//食物最低分数
			{
				ps->SleepTime += 30;
				ps->FoodWeight -= 2;
			}
		}
		//走一步
		SnakeMove(ps);
		
		//睡眠一下
		Sleep(ps->SleepTime);
	 
	} while (ps->status==OK);


}
//游戏结束善后
void GameEnd(pSnake ps)
{
	SetPos(25, 13);
	switch (ps->status)
	{
	case ESC:
		printf("主动退出游戏，正常退出\n");
		break;
	case KILL_BY_WALL:
		printf("很遗憾，撞墙了，游戏结束\n");
		break;
	case KILL_BY_SELF:
		printf("很遗憾，咬到自己了，游戏结束\n");
		break;
	}
	//释放贪吃蛇和链表的资源
	pSnakeNode cur = ps->pSnake;
	pSnakeNode del = NULL;

	while (cur)
	{
		del = cur;
		cur = cur->next;
		free(del);
	}
	free(ps->pFood);
	ps->pSnake = NULL;
	//ps=NULL;
}